package com.ruoyi.web.controller.comlbs;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SimCard;
import com.ruoyi.system.domain.request.equipment.BatchUpdateParam;
import com.ruoyi.system.domain.request.equipment.DeviceIdExistParam;
import com.ruoyi.system.domain.request.equipment.GetSimCardInfoParam;
import com.ruoyi.system.domain.request.equipment.SelfCheckParam;
import com.ruoyi.system.domain.vo.equipment.EquipmentInfo;
import com.ruoyi.system.service.IEquipmentManagementService;
import com.ruoyi.system.service.ISimCardService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备管理Controller
 *
 * @author wp
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/comlbs/equipmentManagement")
public class EquipmentManagementController extends BaseController {

    @Autowired
    private IEquipmentManagementService equipmentManagementService;
    @Autowired
    private ISimCardService simCardService;

    /**
     * 查询设备管理列表
     */
    @ApiOperation("设备管理列表")
    @ApiOperationSupport(includeParameters = {"activateEndTime", "activateStartTime", "companyId", "deviceId", "numberPlate",
            "objType", "checkDetail", "accountStatus"})
    @GetMapping("/list")
    public TableDataInfo list(EquipmentInfo param) {
        startPage();
        List<EquipmentInfo> list = equipmentManagementService.selectList(param);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @ApiOperation("设备管理列表导出")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @ApiOperationSupport(includeParameters = {"activateEndTime", "activateStartTime", "companyId", "deviceId", "numberPlate", "objType", "checkDetail", "accountStatus"})
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentInfo param) {
        List<EquipmentInfo> list = equipmentManagementService.selectList(param);
        ExcelUtil<EquipmentInfo> util = new ExcelUtil<>(EquipmentInfo.class);
        util.hideColumn("updateTime");
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(equipmentManagementService.selectOne(id));
    }

    @ApiOperation("新增设备")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentInfo param) {
        equipmentManagementService.insertEquipment(param);
        return success();
    }

    @ApiOperation("编辑设备")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentInfo param) {
        equipmentManagementService.updateEquipment(param);
        return success();
    }

    @ApiOperation("删除设备")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        equipmentManagementService.deleteEquipment(ids);
        return success();
    }

    @ApiOperation("根据设备号获取车牌号和设备号")
    @GetMapping("/queryDeviceIdAndNumberPlateByDevice")
    public AjaxResult queryDeviceIdAndNumberPlateByDevice(@RequestParam String deviceId) {
        return success(equipmentManagementService.listNumberPlateByDeviceId(deviceId));
    }

    @ApiOperation("设备自检")
    @PostMapping("/selfCheck")
    public AjaxResult selfCheck(@Validated @RequestBody SelfCheckParam param) {
        return success(equipmentManagementService.equipmentCheck(param.getIds()));
    }

    @ApiOperation("转移所属公司")
    @PostMapping("/batchUpdateCompany")
    public AjaxResult batchUpdateCompany(@Validated @RequestBody BatchUpdateParam param) {
        equipmentManagementService.batchUpdateCompany(param);
        return success();
    }

    @ApiOperation("设备管理列表导入")
    @PostMapping("/importData")
    public AjaxResult importData(@RequestPart MultipartFile file, @RequestParam Long companyId) throws Exception {
        equipmentManagementService.importData(file, companyId);
        return success();
    }

    @ApiOperation("设备号重复校验")
    @PostMapping("/checkExistDeviceId")
    public AjaxResult checkExistDeviceId(@Validated @RequestBody DeviceIdExistParam param) {
        equipmentManagementService.checkExistDeviceId(param.getDeviceId(), param.getEquipmentId());
        return success();
    }

    @ApiOperation("SIM卡校验")
    @PostMapping("/checkExistSimId")
    public AjaxResult checkExistSimId(@Validated @RequestBody GetSimCardInfoParam param) {
        equipmentManagementService.checkExistSimId(param.getIccid(), param.getEquipmentId());
        SimCard simCard = simCardService.getSimCardInfo(param.getIccid());
        return success(simCard);
    }
}
